package com.gcd.tempe;

import org.json.JSONException;
import org.json.JSONObject;

import com.gcd.tempe.model.UserLogin;
import com.gcd.tempe.util.GlobalVariabel;
import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	public static String username = null;
	public static String password = null;
	
	  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
    
    public void sendLogin(View v) throws JSONException {
    	//login
    	username = ((EditText) findViewById(R.id.editusername)).getText().toString();
    	password = ((EditText) findViewById(R.id.editpassword)).getText().toString();
    	String response = JSON_Response.getJSONResponse(JSON_Response.DO_LOGIN + username + 
    			"&password=" + password);
    	
    	//if berhasil:
    	if (response.equals("success")) {
    		//Set UserLoginInfo:
    		  //Retrieve UserInfo first :
    		JSONObject responseUserInfo = JSON_Response.getUser(username);
    		String fullname = responseUserInfo.getString("nama_lengkap");
    		String nickname = responseUserInfo.getString("nama_panggilan");
    		String telephoneNumber = responseUserInfo.getString("no_telepon");
    		String address = responseUserInfo.getString("alamat");
    		String point = responseUserInfo.getString("point");
    		  //Create Object UserLoginInfo :
    		GlobalVariabel.userLoginInfo = new UserLogin(username, password, fullname, nickname, telephoneNumber, address, point);
    		
    		Toast.makeText(LoginActivity.this, "Login Berhasil!", Toast.LENGTH_LONG).show();
    		
    		//Set username dan password buat 
        	Intent intent = new Intent(this, MainTEMPE.class);
        	startActivity(intent);
    	} else {
    		Toast.makeText(LoginActivity.this, "Login Gagal!", Toast.LENGTH_LONG).show();
    		username = null;
    		password = null;
    	}
    }
}
